package cn.t.repository.impl.order;

import cn.t.converter.order.ReconConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.order.ReconDO;
import cn.t.persistence.entity.order.ReconPO;
import cn.t.persistence.mapper.order.ReconMapper;
import cn.t.repository.order.ReconRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 对账单表(Recon)数据仓库
 *
 * @author t
 * @since 2024-05-24 10:18:10
 */
@Slf4j
@Repository
@AllArgsConstructor
public class ReconRepositoryImpl extends RepositoryImpl<ReconMapper, ReconPO, ReconConverter, ReconDO> implements ReconRepository {


    @Override
    public List<ReconDO> findByModel(ReconDO reconDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<ReconPO> list = getWrapper(reconDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<ReconPO> getWrapper(ReconDO reconDO) {
        LambdaQueryChainWrapper<ReconPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(reconDO.getReconSn())) {
            wrapper.eq(ReconPO::getReconSn, reconDO.getReconSn());
        }
        if (!ObjectUtils.isEmpty(reconDO.getFirmId())) {
            wrapper.eq(ReconPO::getFirmId, reconDO.getFirmId());
        }
        if (!ObjectUtils.isEmpty(reconDO.getStartDateStart())) {
            wrapper.ge(ReconPO::getGmtStart, reconDO.getStartDateStart());
        }
        if (!ObjectUtils.isEmpty(reconDO.getStartDateEnd())) {
            wrapper.le(ReconPO::getGmtStart, reconDO.getStartDateEnd());
        }
        if (!ObjectUtils.isEmpty(reconDO.getEndDateStart())) {
            wrapper.ge(ReconPO::getGmtEnd, reconDO.getEndDateStart());
        }
        if (!ObjectUtils.isEmpty(reconDO.getEndDateEnd())) {
            wrapper.le(ReconPO::getGmtEnd, reconDO.getEndDateEnd());
        }
        if (!ObjectUtils.isEmpty(reconDO.getGmtCreatedStart())) {
            wrapper.ge(ReconPO::getGmtCreated, reconDO.getGmtCreatedStart());
        }
        if (!ObjectUtils.isEmpty(reconDO.getGmtCreatedEnd())) {
            wrapper.le(ReconPO::getGmtCreated, reconDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(reconDO.getGmtPushCustomer())) {
            wrapper.eq(ReconPO::getGmtPushCustomer, reconDO.getGmtPushCustomer());
        }
        if (!ObjectUtils.isEmpty(reconDO.getPushCustomerStatus())) {
            wrapper.eq(ReconPO::getPushCustomerStatus, reconDO.getPushCustomerStatus());
        }
        if (!ObjectUtils.isEmpty(reconDO.getPushCustomerResult())) {
            wrapper.eq(ReconPO::getPushCustomerResult, reconDO.getPushCustomerResult());
        }
        if (!ObjectUtils.isEmpty(reconDO.getIsPushOms())) {
            wrapper.eq(ReconPO::getIsPushOms, reconDO.getIsPushOms());
        }

        return wrapper;
    }

    @Override
    public long count(ReconDO reconDO) {
        return getWrapper(reconDO).count();
    }
}
